The present invention relates to computer systems and more particularly to a method for managing memory in a run-time environment.
A dynamic run-time environment for a programming language, such as JAVA(trademark), is responsible for managing memory for objects that are created and destroyed during the execution of a program. An object is an entity that encapsulates data and, in some languages, operations associated with the object. Since the encapsulated data is stored in memory, objects are associated with particular regions of memory that are allocated and deallocated by the dynamic run-time environment.
The state of a program, or xe2x80x9cprogram state,xe2x80x9d is the set of the objects and the references between the objects that exist at a specific point in time during the execution of the program. A xe2x80x9creferencexe2x80x9d is used by a run-time environment to identify and ultimately access the region of memory for storing the data of the object. Typically, references between objects in a run-time environment are encoded using machine pointers. A machine pointer is an instance of a native type that contains the address of the object in the main memory, which can be a real memory address or, more commonly, a virtual address on a machine that implements a virtual memory system.
One popular run-time environment is a JAVA(trademark) programming language virtual machine, which supports a platform-independent, object-oriented language developed by Sun Microsystems. In the JAVA(trademark) programming language, the attributes and methods for a class of objects are typically defined in a source file, which is compiled into an architecture-neutral object file containing bytecodes that are interpreted in the virtual machine at the target platform. It is common for objects to reference other objects.
Lately, there has been much interest in using the JAVA(trademark) programming language in a multi-user environment that allows multiple users to connect in separate, concurrent sessions to a server system, such as a relational database system. When designing a run-time environment for such a multi-user environment, scalability in terms of the number of simultaneous users who can establish separate sessions is very important. A significant constraint for user scalability is the size of the memory footprint that each session consumes. For example, a server system may have 100 megabytes (Mb) of memory for supporting all the user sessions. If the session memory footprint is 1 Mb, then only 100 users can be supported at one time. Therefore, it is desirable to reduce the session memory footprint to improve scalability.
One approach for reducing the session memory footprint is to provide a shorter duration memory named xe2x80x9ccall memoryxe2x80x9d that is active for the duration of a xe2x80x9ccallxe2x80x9d but automatically deallocated when the call terminates A call is the period of time, when the user is actively using the server, such as during execution of a Structured Query Language (SQL) statement in a database server. Accordingly, those objects that do not need to live beyond the duration of the call are placed in the call memory rather than session memory. When the call is completed, objects in the call memory are deallocated and the call-duration memory is reclaimed for use. This approach has been implemented in Oracle Corporation""s PL/SQL(trademark) language, for instance, in which objects are explicitly declared as having the duration of a call or of a session. Memory management in such a language is straightforward because the objects are simply allocated in the memory that corresponds to their duration.
The JAVA(trademark) programming language, however, defines the lifetime of many objects, especially system objects, to extend throughout the duration of a session and does not have the notion of a call or call duration in the programming model. Therefore, the run-time environment mush have a policy for using call memory. Accordingly, one approach is to simply ignore the provision of the call memory by the multi-user system and allocate every object in session memory, but this approach suffers from scalability because short-lived objects are unnecessarily allocated in session memory.
Another approach is to allocate objects first in the shorter-duration call memory, and then, at the time the call terminates, migrate the objects into the longer duration session memory. In this approach, session memory is consumed only if the object is still alive at the end of the call. The JAVA(trademark) programming language, however, imposes restrictions on this approach by requiring several system classes to store large objects in static class variables. Use of static class variables is also common in user code. In many cases, the state of the these objects is no longer relevant after the call, but they are still considered alive at the end of the call. Therefore, the above-described migration policy causes these large objects to be migrated into session memory even though the state of the large object is no longer relevant, thereby increasing session memory requirements per session and reducing scalability.
For example, the standard input, output, and error streams maintain large input/output (I/O) buffers that merely reserve space for temporary use by the streams. Typically, when the call terminates the I/O buffers are flushed so that no meaningful information remains in the buffer. However, the I/O buffer itself is still a live object, being referenced by its stream class. Therefore, the migration process of copying a stream object at the end of the call to session memory forces the copying of a large, but essentially empty object into session space.
In this example, there is a problem if the user directly attempts to reduce the session memory consumed by these objects by unreferencing the I/O buffer before the end of the call. Unreferencing an object causes the garbage collector to deallocate the object at the end of the call. If the I/O buffer was unreferenced, the I/O buffer would not be available for use in the finalization of other garbage collected objects at the end of call. Some objects, for example, will print an error message using a stream in their finalization code, which is called when the objects are garbage collected. In this scenario, at worst, the null I/O buffer would cause a run-time error or exception, and, at best, the I/O buffer would be reallocated. In the latter case, the I/O buffer is still alive at the end of call and is therefore migrated to session memory, thereby defeating the purpose of the user""s direct attempt to reduce the session memory usage.
The space used by such objects is substantial, on the order of 150 Kb for a JAVA(trademark) programming language xe2x80x9cHello Worldxe2x80x9d program, thereby seriously affecting the scalability of the system. Therefore, there is a need for improving the scalability of such run-time environments.
This and other needs are addressed by the present invention by providing a mechanism for registering a routine to deinitialize the large object in a class static variable at the end of call (e.g. by setting the class variable to null to unreference the large object), which prevents the large object from being migrated into session memory. This routine is called after the finalization of garbage collected objects that may use the large object. In a subsequent call, the large object is checked before use and re-initialized if the large object was still deallocated. In the stream example, after finalization of garbage collected objects, the I/O buffer is deinitialized, thereby preventing the I/O buffer from being copied into session memory. When the stream is later used in another call, the I/O buffer is checked and, if null, re-initialized.
Accordingly, one aspect of the invention relates to a method and software for managing memory in a run-time environment by registering a routine to deinitialize an object at an end of a call, and, at the end of the call, executing the routine to deinitialize the object. This method may be implemented in a context in which live objects are migrated from a shorter-duration (e.g. call) memory to a longer-duration (e.g. session) memory. In one embodiment, the routine is invoked after finalizing or otherwise cleaning up non-live objects during garbage collection at the end of the call. In an embodiment, an enclosing object that contains a reference to the object (e.g. a stream object enclosing the I/O buffer) is also registered as a key, to facilitate garbage collection of the object.
Another aspect of the invention relates to a method and software for managing memory in a run-time environment. Before use of an object, the object is checked to determine if the object is uninitialized and, if the object is uninitialized (e.g. null), then the object is initialized; and a routine is registered to deinitialize the object at an end of a call. At the end of the call, the routine is executed to deinitialize the object (e.g. by setting the object to null). In one embodiment, the routine is invoked after finalizing non-live objects during garbage collection at the end of the call.
Still other objects and advantages of the present invention will become readily apparent from the following detailed description, simply by way of illustration of the best mode contemplated of carrying out the invention. As will be realized, the invention is capable of other and different embodiments, and its several details are capable of modifications in various obvious respects, all without departing from the invention. Accordingly, the drawing and description are to be regarded as illustrative in nature, and not as restrictive.